package cs348a.data;

/**
 * <code>CurveEvaluator</code> is used to evaluate a curve.
 * TODO: only works for quadratic curves. expand to the general case
 * 
 * @author H.S Kim
 */
public class CurveEvaluator {
	private Site site1 = null;
	private Site site2 = null;
	private Site site3 = null;
	
	/**
	 * Set the bezier control points
	 * 
	 * @param s1
	 * @param s2
	 * @param s3
	 */
	public void setControlPoints(Site s1, Site s2, Site s3) {
		site1 = s1;
		site2 = s2;
		site3 = s3;
	}
	
	/**
	 * Evaluate the curve
	 * 
	 * @param t - point to evaluate. t is in the range [0, 1]
	 * @return
	 */
	public float[] evaluate(float t) {
		// check if control points have been set.
//		if(site1 == null || site2 == null || site3 == null) {
//			throw new Exception("Sites have not been set");
//		}
//		if(t < 0 || t > 1) {
//			throw new Exception("Invalid value - t: " + t);
//		}
		
		float b0 = (1-t)*(1-t);
		float b1 = 2*t*(1-t);
		float b2 = t*t;
		
		return new float[]{
				site1.x*b0 + site2.x*b1 + site3.x*b2, 
				site1.y*b0 + site2.y*b1 + site3.y*b2, 
				site1.z*b0 + site2.z*b1 + site3.z*b2};
	}
}
